
package example

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/example"
    exampleReq "github.com/flipped-aurora/gin-vue-admin/server/model/example/request"
    "gorm.io/gorm"
)

type VideoAudioLibraryService struct {}
// CreateVideoAudioLibrary 创建videoAudioLibrary表记录
// Author [yourname](https://github.com/yourname)
func (videoAudioLibraryService *VideoAudioLibraryService) CreateVideoAudioLibrary(ctx context.Context, videoAudioLibrary *example.VideoAudioLibrary) (err error) {
	err = global.GVA_DB.Create(videoAudioLibrary).Error
	return err
}

// DeleteVideoAudioLibrary 删除videoAudioLibrary表记录
// Author [yourname](https://github.com/yourname)
func (videoAudioLibraryService *VideoAudioLibraryService)DeleteVideoAudioLibrary(ctx context.Context, ID string,userID uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
	    if err := tx.Model(&example.VideoAudioLibrary{}).Where("id = ?", ID).Update("deleted_by", userID).Error; err != nil {
              return err
        }
        if err = tx.Delete(&example.VideoAudioLibrary{},"id = ?",ID).Error; err != nil {
              return err
        }
        return nil
	})
	return err
}

// DeleteVideoAudioLibraryByIds 批量删除videoAudioLibrary表记录
// Author [yourname](https://github.com/yourname)
func (videoAudioLibraryService *VideoAudioLibraryService)DeleteVideoAudioLibraryByIds(ctx context.Context, IDs []string,deleted_by uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
	    if err := tx.Model(&example.VideoAudioLibrary{}).Where("id in ?", IDs).Update("deleted_by", deleted_by).Error; err != nil {
            return err
        }
        if err := tx.Where("id in ?", IDs).Delete(&example.VideoAudioLibrary{}).Error; err != nil {
            return err
        }
        return nil
    })
	return err
}

// UpdateVideoAudioLibrary 更新videoAudioLibrary表记录
// Author [yourname](https://github.com/yourname)
func (videoAudioLibraryService *VideoAudioLibraryService)UpdateVideoAudioLibrary(ctx context.Context, videoAudioLibrary example.VideoAudioLibrary) (err error) {
	err = global.GVA_DB.Model(&example.VideoAudioLibrary{}).Where("id = ?",videoAudioLibrary.ID).Updates(&videoAudioLibrary).Error
	return err
}

// GetVideoAudioLibrary 根据ID获取videoAudioLibrary表记录
// Author [yourname](https://github.com/yourname)
func (videoAudioLibraryService *VideoAudioLibraryService)GetVideoAudioLibrary(ctx context.Context, ID string) (videoAudioLibrary example.VideoAudioLibrary, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&videoAudioLibrary).Error
	return
}
// GetVideoAudioLibraryInfoList 分页获取videoAudioLibrary表记录
// Author [yourname](https://github.com/yourname)
func (videoAudioLibraryService *VideoAudioLibraryService)GetVideoAudioLibraryInfoList(ctx context.Context, info exampleReq.VideoAudioLibrarySearch) (list []example.VideoAudioLibrary, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&example.VideoAudioLibrary{})
    var videoAudioLibrarys []example.VideoAudioLibrary
    // 如果有条件搜索 下方会自动创建搜索语句
    if len(info.CreatedAtRange) == 2 {
     db = db.Where("created_at BETWEEN ? AND ?", info.CreatedAtRange[0], info.CreatedAtRange[1])
    }
    
    if info.AudioTitle != nil && *info.AudioTitle != "" {
        db = db.Where("audio_title LIKE ?", "%"+ *info.AudioTitle+"%")
    }
    if info.AudioIp != nil && *info.AudioIp != "" {
        db = db.Where("audio_ip LIKE ?", "%"+ *info.AudioIp+"%")
    }
    if info.AudioUrl != nil && *info.AudioUrl != "" {
        db = db.Where("audio_url LIKE ?", "%"+ *info.AudioUrl+"%")
    }
    if info.AuditStatus != nil && *info.AuditStatus != "" {
        db = db.Where("audit_status = ?", *info.AuditStatus)
    }
    if info.Reviewer != nil {
        db = db.Where("reviewer = ?", *info.Reviewer)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }

	err = db.Find(&videoAudioLibrarys).Error
	return  videoAudioLibrarys, total, err
}
func (videoAudioLibraryService *VideoAudioLibraryService)GetVideoAudioLibraryPublic(ctx context.Context) {
    // 此方法为获取数据源定义的数据
    // 请自行实现
}
